Page({
  data: {
    accounts: [
      {
        id: 1,
        avatarUrl: '/images/mine/avatar1.png',
        nickname: '用户A',
        account: 'userA@example.com',
        isCurrent: true,
        isAdmin: false
      },
      {
        id: 2,
        avatarUrl: '/images/mine/avatar2.png',
        nickname: '用户B',
        account: 'userB@example.com',
        isCurrent: false,
        isAdmin: true
      }
    ]
  },

  onLoad() {
    // 从本地存储加载账号列表
    const storedAccounts = wx.getStorageSync('loggedAccounts');
    if (storedAccounts && storedAccounts.length > 0) {
      this.setData({ accounts: storedAccounts });
    }
  },

  onShow() {
    // 每次页面显示时重新加载账号数据
    this.onLoad();
  },

  // 切换当前账号
  onSwitchAccount(e) {
    const accountId = e.currentTarget.dataset.accountid;
    const newAccounts = this.data.accounts.map(account => {
      return {
        ...account,
        isCurrent: account.id === accountId
      };
    });
    
    this.setData({ accounts: newAccounts });
    wx.setStorageSync('loggedAccounts', newAccounts);
    
    // 提示并返回上一页
    wx.showToast({
      title: '切换账号成功',
      icon: 'success',
      duration: 1500
    });
    
    setTimeout(() => {
      wx.navigateBack({
        delta: 1
      });
    }, 1500);
  },

  // 添加新账号
  onAddAccount() {
    wx.navigateTo({
      url: '/pages/login/login'
    });
  },

  // 删除账号
  onDeleteAccount(e) {
    const accountId = e.currentTarget.dataset.accountid;
    const accountToDelete = this.data.accounts.find(account => account.id === accountId);
    
    wx.showModal({
      title: '确认删除',
      content: `确定要删除账号 ${accountToDelete.nickname} 吗？`,
      confirmColor: '#ff4d4f',
      success: (res) => {
        if (res.confirm) {
          // 检查是否删除当前账号
          const isDeletingCurrent = accountToDelete.isCurrent;
          
          // 过滤掉要删除的账号
          let newAccounts = this.data.accounts.filter(account => account.id !== accountId);
          
          // 如果删除的是当前账号且还有其他账号，设置第一个为当前账号
          if (isDeletingCurrent && newAccounts.length > 0) {
            newAccounts[0].isCurrent = true;
          }
          
          this.setData({ accounts: newAccounts });
          wx.setStorageSync('loggedAccounts', newAccounts);
          
          wx.showToast({
            title: '账号已删除',
            icon: 'success',
            duration: 1500
          });
        }
      }
    });
  },

  // 设置管理员
  onSetAdmin(e) {
    const accountId = e.currentTarget.dataset.accountid;
    
    wx.showModal({
      title: '设置管理员',
      content: '每个账号组只能有一个管理员，确定要将此账号设为管理员吗？',
      success: (res) => {
        if (res.confirm) {
          // 将所有账号的isAdmin设为false，然后将目标账号设为true
          const newAccounts = this.data.accounts.map(account => {
            return {
              ...account,
              isAdmin: account.id === accountId
            };
          });
          
          this.setData({ accounts: newAccounts });
          wx.setStorageSync('loggedAccounts', newAccounts);
          
          wx.showToast({
            title: '管理员设置成功',
            icon: 'success',
            duration: 1500
          });
        }
      }
    });
  }
});
    